<!--
 * @Author: ningjz
 * @Date: 2022-04-11 18:05:46
 * @Description: 价格产销库存
-->
<template>
  <com-box :title="data.title" style="margin-top: 15px">
    <template v-slot>
      <loading v-show="data.loading" />
      <div class="nav">
        <div class="box-bar echart-item">
          <div class="jgdb-unit">{{ data.unit }}</div>
          <!-- <charts class="line-chart" :params="data.params"></charts> -->
          <div ref="chart" style="width: 60%; height: 310px"></div>
          <div class="line-table">
            <div class="Header">
              <span>{{ data.header.name }}</span>
              <span>{{ data.header.value1 }}</span>
              <span>{{ data.header.value2 }}</span>
            </div>
            <div class="main">
              <ul>
                <li
                  v-for="(item, index) in data.list"
                  :key="index"
                  :style="{ background: item.background }"
                >
                  <span class="name">
                    {{ item.name }}
                  </span>
                  <span>
                    <p v-if="item.value1" style="text-align: right">
                      {{ item.value1 }}
                    </p>
                  </span>
                  <span>
                    <p v-if="item.value2" style="text-align: right">
                      {{ item.value2 }}
                    </p>
                  </span>
                </li>
                <div
                  v-for="(item, index) in data.list"
                  :key="index"
                  class="table-main"
                >
                  <template v-if="item.children">
                    <div class="table-line"></div>
                    <div
                      v-for="(a, b) in item.children"
                      :key="b"
                      class="table-li"
                    >
                      <span class="name">
                        <span class="name-line">--</span>
                        {{ a.name }}
                      </span>
                      <span>
                        <p v-if="a.value1" style="text-align: right">
                          {{ a.value1 }}
                        </p>
                      </span>
                      <span>
                        <p v-if="a.value2" style="text-align: right">
                          {{ a.value2 }}
                        </p>
                      </span>
                    </div>
                  </template>
                </div>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </template>
  </com-box>
</template>
<script lang="ts" setup>
import comBox from '@/components/comBox/comBox2.vue'
import loading from '@/charts/loading.vue'
import {
  defineComponent,
  reactive,
  onMounted,
  onBeforeUnmount,
  ref,
  watch,
  toRefs,
  defineExpose,
  defineEmits,
  defineProps
} from 'vue'
import httpRequest from '@/service'
import * as echarts from 'echarts'
const emits = defineEmits(['change'])
const props = defineProps({
  date: String
})
const chart = ref(null)
let myChart: any
const getData = () => {
  const dom = chart.value
  if (dom) {
    if (myChart !== null && myChart !== '' && myChart !== undefined) {
      myChart.dispose()
    }
    myChart = echarts.init(dom)
  }
  // const myChart = echarts.init(chart.value)
  const option = {
    tooltip: {
      show: true,
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
        snap: true
      },
      confine: true,
      backgroundColor: '#1959a7',
      borderColor: '#4beeff',
      borderWidth: 1,
      padding: 12,
      textStyle: {
        color: '#fff',
        fontSize: 14
      }
    },
    title: {
      show: true,
      text: '',
      left: '20',
      top: '10',
      right: '20',
      textStyle: {
        fontSize: 18,
        fontFamily: 'Microsoft YaHei',
        color: 'rgba(162, 199, 255,1)'
      }
    },
    legend: {
      show: false
    },
    grid: {
      containLabel: true,
      top: 50,
      right: 10,
      bottom: 10,
      left: 10
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 735, name: '水电' },
          { value: 484, name: '光伏' },
          { value: 580, name: '风电' },
          { value: 1048, name: '火电' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        },
        itemStyle: {
          color: function (colors: any) {
            var colorList = ['#fff100', '#82ef52', '#0066ff', '#ff712b']
            return colorList[colors.dataIndex]
          }
        },
        labelLine: {
          length: 40,
          length2: 30,
          lineStyle: {
            color: 'rgb(29, 82, 197)',
            width: 2
          }
        },
        label: {
          textStyle: {
            color: '#fff',
            fontSize: '16px'
          },
          formatter: (d: { name: any; value: any; data: { value2: any } }) => {
            return `{name|${d.name}}\n{zhi|${d.value}}亿\n{zhi|${d.data.value2}}`
          },
          rich: {
            name: {
              fontWeight: 'bold',
              fontSize: '16px',
              lineHeight: 20,
              align: 'left'
            },
            zhi: {
              fontSize: '14px',
              lineHeight: 20,
              align: 'left'
            }
          }
        }
      }
    ]
  }
  data.loading = true
  data.list = []
  httpRequest
    .get({ url: 'M2P2FDL', params: { year: props.date } })
    .then((res) => {
      if (res.code == 200 && Object.keys(res.map).length > 0) {
        data.list = res.list
        data.header = res.map
        data.unit = res.unit
        data.title = res.title
        const arr: any[] = []
        res.data.forEach((i: { value: any; value1: any }) => {
          i.value = i.value1
          arr.push(i)
        })
        option.series[0].data = arr
        myChart.setOption(option)
        data.loading = false
      }
    })
    .catch(() => {
      console.log('请求失败')
    })
}
const data: any = reactive({
  visible: false,
  title: '',
  unit: '',
  header: {},
  list: []
})
onMounted(() => {
  getData()
})
const refreshData = () => {
  getData()
}
defineExpose({
  refreshData
})
</script>
<style lang="less" scoped>
.nav {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  // background-color: rgba(33, 122, 255, 0.1);
  .echart-item {
    width: 100%;
  }
  .box-bar {
    height: 100%;
    padding: 7px 30px 7px 15px;
    position: relative;
    display: flex;
    .jgdb-unit {
      position: absolute;
      font-size: 18px;
      color: rgb(70, 237, 232);
      top: 0;
      left: 7px;
    }
    .jgdb-img {
      position: absolute;
      top: -20px;
      left: 7px;
    }
  }
  ::v-deep(.com-box .slot-box) {
    height: calc(100% - 55px);
  }
  .line-table {
    margin-top: 30px;
    width: 40%;
    height: 310px;
    ul {
      width: 100%;
      height: 100%;
      padding: 0 10px;
      .table-main {
        position: relative;
        .table-line {
          position: absolute;
          width: 1px;
          height: 77px;
          background-color: rgb(0, 160, 233);
          left: 45px;
          top: -10px;
        }
        .name {
          text-align: right;
          width: 32%;
          .name-line {
            color: rgb(0, 160, 233);
          }
        }
      }
      .table-li {
        display: flex;
        height: 35px;
        align-items: center;
        justify-content: space-around;
        cursor: pointer;
        margin: 5px 0;
        span {
          width: 25%;
          text-align: center;
        }
        p {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: rgb(214, 235, 255);
          word-break: break-all;
        }
        .name-box {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: rgb(255, 192, 111);
          font-weight: bold;
        }

        .name {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: #fff;
        }
      }
      li {
        display: flex;
        height: 35px;
        align-items: center;
        justify-content: space-around;
        cursor: pointer;
        background: url(../../img/listbg.png) no-repeat center;
        background-size: 100% 100%;
        margin: 5px 0;
        span {
          width: 25%;
          text-align: center;
        }
        p {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: rgb(214, 235, 255);
          word-break: break-all;
        }
        .name-box {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: rgb(255, 192, 111);
          font-weight: bold;
        }

        .name {
          font-size: 14px;
          font-family: 'Microsoft YaHei';
          color: #fff;
        }
      }
    }
  }
  .Header {
    background-color: rgba(30, 90, 214, 0.8) !important;
    height: 41px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin: 0 10px;
    border-radius: 10px;
    span {
      width: 33%;
      white-space: pre-line;
      font-size: 14px;
      font-family: 'Microsoft YaHei';
      color: rgb(222, 255, 255);
      font-weight: bold;
      line-height: 1.714;
      text-align: center;
    }
  }
}
</style>
